package com.ruiyi.subway.utils;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

public class DistanceUtils {
	
	private static double EARTH_RADIUS = 6378.137;
	
	  private static double rad(double d) { 
	        return d * Math.PI / 180.0; 
	    } 
	  /**
	   * 计算距离
	   * @param lat1
	   * @param lng1
	   * @param lat2
	   * @param lng2
	   * @return
	   */
	  public static double getDistance(double lat1, double lng1, double lat2, double lng2) { 
	        double radLat1 = rad(lat1); 
	        double radLat2 = rad(lat2); 
	        double a = radLat1 - radLat2; 
	        double b = rad(lng1) - rad(lng2); 
	        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) 
	                + Math.cos(radLat1) * Math.cos(radLat2) 
	                * Math.pow(Math.sin(b / 2), 2))); 
	        s = s * EARTH_RADIUS; 
	        BigDecimal bg = new BigDecimal(s);
	        double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
	        return f1;
	    }
	  
	  public static boolean compareDistance(List<Map> list,double lat1, double lng1,Integer in){
		  //LONGITUDE=106.47589, LATITUDE=29.675211
		  int count = 0;
		  for(int i=0;i<list.size();i++){
			  Map map = list.get(i);
			  double lat2 = Double.parseDouble(map.get("LATITUDE").toString());
			  double lng2 = Double.parseDouble(map.get("LONGITUDE").toString());
			  double result = getDistance(lat1, lng1, lat2, lng2);
			  if(result > in){
				  count++;
			  }
		  }
		  if(count == 3){
			  return true;
		  }else{
			  return false;
		  }
	  }
}
